#include "Task_OSD.h"
#include "at7456e.h"
#include "stdbool.h"
#include "math.h"
#include <stdio.h>




/* OSD ????? */
#define OSD_TASK_PRIO 16
/* OSD ?????? */
#define OSD_STK_SIZE 256
/* OSD ?????? */
TaskHandle_t OSDTask_Handler;
QueueHandle_t OsdRefresh_Sem;
//QueueHandle_t Osd_GpsModule_sem;
QueueHandle_t Osd_ClearScreen_QueueHandle;//清屏
QueueHandle_t ForewardReverse_QueueHandle;//电机正反转
QueueHandle_t Photo_QueueHandle;//拍照
QueueHandle_t Cursor_QueueHandle; //光标
QueueHandle_t Distance_QueueHandle;//激光测距
QueueHandle_t Zoom_QueueHandle;
QueueHandle_t Laser_QueueHandle;//激光图标
_BatteryState BatteryState;
/* OSD ?????? */
void OSD_task(void *pvParameters);


void task_OSD_Create(void)
{
	//??OSD??
    xTaskCreate((TaskFunction_t )OSD_task,
                (const char*    )"OSD_task",
                (uint16_t       )OSD_STK_SIZE,
                (void*          )NULL,
                (UBaseType_t    )OSD_TASK_PRIO,
                (TaskHandle_t*  )&OSDTask_Handler);
								
		OsdRefresh_Sem = xSemaphoreCreateBinary();		//Osd场刷新信号量
		Osd_ClearScreen_QueueHandle = xQueueCreate(1,sizeof(uint16_t)); //清屏消息队列
		ForewardReverse_QueueHandle = xQueueCreate(1,sizeof(uint16_t)); //电机正反转消息队列
		Photo_QueueHandle = xQueueCreate(1,sizeof(uint16_t)); //拍照消息队列
		Cursor_QueueHandle = xQueueCreate(1,sizeof(_Bool)); //光标清屏队列
		Distance_QueueHandle = xQueueCreate(Usart2_RXBuffSize, Usart2_RXBuffSize*sizeof(uint8_t)); //激光测距队列
		Laser_QueueHandle = xQueueCreate(1, sizeof(uint8_t)); //激光图标队列
		Zoom_QueueHandle = xQueueCreate(1, sizeof(_Bool));
								
								
}


typedef enum 
{
	_JustClearScreen,//只清屏
	_ClearScreenAndShowMainMenu,//清屏后显示主菜单
}_ClearQueueFunction;

CHAR_ATTRIB osd_px;
unsigned char ererter,rwerr;
int retyy;
/* LED0 ???? */
void OSD_task(void *pvParameters)
{
	_ClearQueueFunction ClearOsd_queue;
	_OsdRun_Mode _OsdRunMode_queue;
	

	
	
	//turnOff_osd();
	vTaskDelay(10);
	//write_at7456E_font(0, charset_array_NONE);
	vTaskDelay(10);
	

	at7456e_refresh_exint_init();
	Init_OsdRefresh_Family(OsdRefresh_Family);//初始化各显示单元的位置

	

#if BT656 
	StartIRTimer();
#endif	

	while(1)
	{		

//		BT656_Init();
		//read_at7456E_font(0x31,&ererter);
		
		if(xSemaphoreTake(OsdRefresh_Sem, portMAX_DELAY) == pdTRUE)
		{
			if(pdTRUE == xQueueReceive(Osd_ClearScreen_QueueHandle,&ClearOsd_queue,0))//是否清屏
			{
				if(ClearOsd_queue == _JustClearScreen)
					clear_screen();
			}

			//光标移动准备
			CursorMove_OsdShow();
			
			//电池电量展示
			BatteryShow();
			
//			//显示电机正反转图标(红外模式下调焦)
//			if(_MotorStop != xQueueReceive(ForewardReverse_QueueHandle,&ForwardReverse_queue,0))
//			{
//				OsdRefresh_Family[_ForwardReverse].Refresh_Flag = Refresh_yes;
//			}
			
			//倍数缩放图标
			Zoom_OsdShow();
			
			//检查更新
			CheckOsdReresh(OsdRefresh_Family);
			
			//显示距离（激光测距）
			OsdShowDistance();
			
			//录像闪烁
			RecordIconBlink(OsdRun_Mode);
			
			
#if Control_Num_Blink_Flag			
			//二级菜单配置闪烁
			Control_Num_Blink(MenuLevel, &ConfigMenuPra);
#endif

			//拍照闪烁
			PhotoIconBlink(OsdRun_Mode);
			
			//使激光图标闪烁
			LaserIconBlink();
			
			//罗盘
			CompassStartMeasure();
			
#if Motor_Flag
			//电机驱动
			MotoForewardReverse_Show_Set();
#endif

		}
	}
}









